<?php
/**
 * Created by PhpStorm.
 * User: xiaoyu
 * Date: 2018/1/24
 * Time: 下午2:50
 */
namespace Model;
use Think\Model;
class GoodsModel extends Model{
    //自动完成设置
    protected $_auto = array(
        array('add_time','time',1,'function'),
        //添加时也更新
        array('upd_time','time',3,'function'),
    );

    //&$data 是引用传递,函数内部改变,外边也可以访问到
    protected function _before_insert(&$data,$options){
        //dump($data);
        //dump($_FILES);

        if ($_FILES['goods_logo']['error'] == 0) {


            $cfg = array(
                'rootPath' => './Common/Uploads/',
            );
            //@mkdir('./Common/Uploads');
            $up = new \Think\Upload($cfg);

//            dump($up);
//
//            exit;
            
            $z = $up->uploadOne($_FILES['goods_logo']);

            //. 拼接
            $big_path_name = $up->rootPath . $z['savepath'] . $z['savename'];
            $data['goods_big_logo'] = $big_path_name;

//            dump($_FILES);
//            dump($z);



            //缩略图
            $im = new \Think\Image();
            $im->open($big_path_name);
            $im->thumb(60,60);

            $small_path_name = $up->rootPath . $z['savepath'] . "small_" .$z['savename'];
            $im->save($small_path_name);

            $data['goods_small_logo'] = $small_path_name;

            //exit;
        }
    }

    //在这里修改$data也不好用了
    protected function _after_insert($data,$options){

        //dump($data);
       //dump($options);

        //exit;

        //dump($_FILES);

        $flag = false;
        foreach ($_FILES['goods_pics']['error'] as $a => $b){
            if ($b === 0){
                $flag = true;
                break;
            }
        }


        if ($flag === true) {


            $cfg = array(
                'rootPath' => './Common/Pics/',
            );


            $up = new \Think\Upload($cfg);
            $z = $up->upload(array('goods_pics' => $_FILES['goods_pics']));

            //dump($z);

            //这个$k是 0 1 2
            foreach ($z as $k => $v) {
                $pics_big_name = $up->rootPath . $v['savepath'] . $v['savename'];

                //制作小图
                //缩略图
                $im = new \Think\Image();
                $im->open($pics_big_name);
                $im->thumb(60, 60);

                $pics_small_name = $up->rootPath . $v['savepath'] . "small_" . $v['savename'];
                $im->save($pics_small_name);


                //goods_id pics_big pics_small

                $arr = array(
                    'goods_id' => $data['goods_id'],
                    'pics_big' => $pics_big_name,
                    'pics_small' => $pics_small_name,
                );

                //dump($arr);


                //表名 goods_pics
                D('GoodsPics')->add($arr);

            }


        }


    }

    function fetchData(){
        $total = $this->count();
        $per = 5;

        $page = new \Common\Tools\Page($total,$per);

        $pageinfo = $this->where(array('is_del'=>'不删除'))->order('goods_id desc')->limit($page->offset,$per)->select();
        $pagelist = $page->fpage();
        //$pagelist = $page->fpage(array(3,4,5,6,7,8));
        return array(
            'pageinfo' => $pageinfo,
            'pagelist' => $pagelist
        );
    }
    protected function _before_update(&$data,$options){

        if ($_FILES['goods_logo_upd']['error'] == 0) {


            //dump($data);
            //dump($options);

            //删除原图片
            //$data['goods_id'] 不对

            $logoinfo = $this->field('goods_big_logo,goods_small_logo')->find($options['where']['goods_id']);
            if (!empty($logoinfo['goods_big_logo']) || !empty($logoinfo['goods_small_logo'])){


                unlink($logoinfo['goods_big_logo']);
                unlink($logoinfo['goods_small_logo']);

//                dump($logoinfo['goods_big_logo']);
//
//                dump($logoinfo);
//                exit;
            }

            //dump($logoinfo);

            //exit;


            $cfg = array(
                'rootPath' => './Common/Uploads/',
            );
            $up = new \Think\Upload($cfg);
            $z = $up->uploadOne($_FILES['goods_logo_upd']);

            //. 拼接
            $big_path_name = $up->rootPath . $z['savepath'] . $z['savename'];
            $data['goods_big_logo'] = $big_path_name;

            //dump($z);


            //缩略图
            $im = new \Think\Image();
            $im->open($big_path_name);
            $im->thumb(60,60);

            $small_path_name = $up->rootPath . $z['savepath'] . "small_" .$z['savename'];
            $im->save($small_path_name);

            $data['goods_small_logo'] = $small_path_name;

            //exit;
        }





        $flag = false;
        foreach ($_FILES['goods_pics_upd']['error'] as $a => $b){
            if ($b === 0){
                $flag = true;
                break;
            }
        }


        if ($flag === true) {
            $cfg = array(
                'rootPath' => './Common/Pics/',
            );

            $up = new \Think\Upload($cfg);
            $z = $up->upload(array('goods_pics_upd' => $_FILES['goods_pics_upd']));
            //dump($z);
            //这个$k是 0 1 2
            foreach ($z as $k => $v) {
                $pics_big_name = $up->rootPath . $v['savepath'] . $v['savename'];

                //制作小图
                //缩略图
                $im = new \Think\Image();
                $im->open($pics_big_name);
                $im->thumb(60, 60);

                $pics_small_name = $up->rootPath . $v['savepath'] . "small_" . $v['savename'];
                $im->save($pics_small_name);
                //goods_id pics_big pics_small

                //这里的goods_id要从$options['where']['goods_id']
                //不是data['goods_id']
                $arr = array(
                    'goods_id' => $options['where']['goods_id'],
                    'pics_big' => $pics_big_name,
                    'pics_small' => $pics_small_name,
                );
                //dump($arr);
                //表名 goods_pics
                D('GoodsPics')->add($arr);
            }
        }



    }
    protected function _after_update($data,$options){

    }


}